<template>
	<div class="flex flex-column login-container">
		<div class="login_top">
			<div class="left_logo"></div>
		</div>
		<div class="login_middle">
			<div class="formClass">
				<el-form ref="form" v-if="showType==='forget'" :model="form" :rules="rules" class="forget_form"
					auto-complete="off">
					<div class="title-container">
						<h3 class="title">忘记密码</h3>
					</div>
					<el-form-item>
						<span class="svg-container">
							<svg-icon :size="16" icon-class="mobile" />
						</span>
						<el-input maxlength="11" v-model="form.mobile" placeholder="请输入手机号" @input="handleMobileChange">
						</el-input>
					</el-form-item>
					<el-form-item style="border:none" prop="code">
						<div class="flex">
							<div class="flex-1 text-left border_box">
								<span class="svg-container" style="padding-left: 8px;">
									<svg-icon :size="16" icon-class="msg" />
								</span>
								<el-input style="width:130px" v-model="form.code" placeholder="请输入验证码"
									autocomplete="off"></el-input>
							</div>
							<div class="inline-block pointer text-center code_btn" type="primary"
								@click="getPhoneCode()">
								{{ showTime ? timeCount + 's后重新获取' : '获取验证码' }}
							</div>
						</div>
					</el-form-item>
					<el-form-item prop="pwd">
						<span class="svg-container">
							<svg-icon :size="16" icon-class="password" />
						</span>
						<el-input v-model="form.pwd" placeholder="请输入新登陆密码" show-password autocomplete="new-password">
						</el-input>
					</el-form-item>
					<el-form-item prop="confirmPwd">
						<span class="svg-container">
							<svg-icon :size="16" icon-class="password" />
						</span>
						<el-input v-model="form.confirmPwd" placeholder="请确认新登陆密码" show-password
							autocomplete="new-password"></el-input>
					</el-form-item>
					<el-button type="primary" style="width:100%" @click.native.prevent="submit">确认</el-button>
					<p class="text-center" style="color:#999999;font-size:16px;line-height:35px"><span class="pointer"
							@click="changeType">返回登录</span></p>
				</el-form>
				<el-form v-else ref="loginForm" :model="loginForm" :rules="loginRules" class="login_form"
					auto-complete="on" label-position="left">
					<div class="title-container">
						<h3 class="title">账户登录</h3>
					</div>

					<el-form-item prop="username">
						<span class="svg-container">
							<svg-icon :size="16" icon-class="username" />
						</span>
						<el-input ref="username" maxlength="11" v-model="loginForm.username" placeholder="手机号"
							name="username" type="text" tabindex="1" auto-complete="on" @input="handleNameChange" />
					</el-form-item>

					<el-form-item prop="password">
						<span class="svg-container">
							<svg-icon :size="16" icon-class="password" />
						</span>
						<el-input ref="password" v-model="loginForm.password" placeholder="密码" name="password"
							tabindex="2" show-password autocomplete="on" @keyup.enter.native="handleLogin" />
					</el-form-item>
					<el-button :loading="loading" type="primary" style="width:100%;margin-bottom:30px;"
						@click.native.prevent="handleLogin">登录</el-button>
					<p class="flex flex-between-center">
						<span class="pointer blue-font" @click="toApply">供应商入驻</span>
						<span class="pointer" @click="changeType">忘记密码</span>
					</p>
				</el-form>
			</div>
		</div>

		<div class="login_footer">
		</div>
	</div>
</template>

<script>
	import {
		checkMobile
	} from '@/utils/validate';
	import {
		forgetPwd,
		sendMobileCode
	} from '@/api/login';
	export default {
		name: 'Login',
		data() {
			var validateConfirm = (rule, value, callback) => {
				if (value !== this.form.pwd) {
					callback(new Error('两次输入密码不同！'));
				} else {
					callback();
				}
			}
			var validatePwd = (rule, value, callback) => {
				let reg = /^[^\u4e00-\u9fa5]{0,}$/;
				if (value.length < 6) {
					callback(new Error('密码必须大于等于6个字符'))
				} else if (!reg.test(value)) {
					callback(new Error('密码不能包含汉字！'));
				} else {
					callback();
				}
			}
			const validateUsername = (rule, value, callback) => {
				if (!checkMobile(value)) {
					callback(new Error('手机号格式错误'));
				} else {
					callback();
				}
			}
			return {
				loginForm: {
					username: '',
					password: ''
				},
				loginRules: {
					username: [{
						required: true,
						trigger: 'blur',
						validator: validateUsername
					}],
					password: [{
						required: true,
						trigger: 'blur',
						validator: validatePwd
					}]
				},
				loading: false,
				passwordType: 'password',
				redirect: undefined,
				hasVerify: false,
				showVerify: false,
				showType: 'login',
				form: {
					mobile: "",
					code: "",
					pwd: "",
					confirmPwd: ""
				},
				rules: {
					mobile: [{
							required: true,
							message: '手机号不能为空',
							trigger: 'blur'
						},
						{
							validator: validateUsername,
							trigger: 'blur'
						}
					],
					code: [{
						required: true,
						message: '验证码不能为空',
						trigger: 'blur'
					}],
					pwd: [{
							required: true,
							message: '密码不能为空',
							trigger: 'blur'
						},
						{
							validator: validatePwd,
							trigger: 'blur'
						}
					],
					confirmPwd: [{
							required: true,
							message: '确认密码不能为空',
							trigger: 'blur'
						},
						{
							validator: validateConfirm,
							trigger: 'blur'
						}
					],
				},
				showTime: false, // 显示60s倒计时
				timeCount: "", // 计时显示
				timer: null // 计时
			}
		},
		watch: {
			$route: {
				handler: function(route) {
					this.redirect = route.query && route.query.redirect
				},
				immediate: true
			}
		},
		methods: {
			toApply() {
				this.$router.push({
					name: 'apply'
				});
			},
			changeType() {
				this.showType = this.showType === 'login' ? 'forget' : 'login';
				this.$nextTick(() => {
					if (this.showType === 'login') {
						this.showVerify = false;
						this.loginForm = {
							username: '',
							password: ''
						}
						this.showTime = false;
						clearInterval(this.timer);
						this.timer = null;
						this.$refs.loginForm.clearValidate();
					} else {
						this.form = {
							mobile: "",
							code: "",
							pwd: "",
							confirmPwd: ""
						}
						this.$refs.form.clearValidate();
					}
				})
			},
			submit() {
				let that = this;
				this.$refs['form'].validate((valid) => {
					if (valid) {
						forgetPwd({
							mobile: this.form.mobile,
							pwd: this.form.pwd ? this.$md5(this.form.pwd) : "",
							code: this.form.code
						}).then(res => {
							this.$alert(
								'<span><i class="el-icon-circle-check success_icon"></i></span><br />修改密码成功！去登录。',
								'', {
									confirmButtonText: '知道了',
									dangerouslyUseHTMLString: true,
									center: true,
									showClose: false,
									closeOnClickModal: false,
									closeOnPressEscape: false,
									customClass: 'success_msgbox',
									callback: action => {
										that.changeType();
									}
								});
						})
					} else {
						this.$message({
							message: "请检查表单错误",
							type: "error"
						});
						return false;
					}
				});
			},
			getCodes(mobile) {
				sendMobileCode(mobile).then((res) => {
					this.showTime = true;
					const TIME_COUNT = 60;
					this.timeCount = 60;
					this.timer = setInterval(() => {
						if (this.timeCount > 0 && this.timeCount <= TIME_COUNT) {
							this.timeCount--;
						} else {
							this.showTime = false;
							clearInterval(this.timer);
							this.timer = null;
						}
					}, 1000)
				});
			},
			getPhoneCode() {
				if (this.showTime === true) {
					return;
				}
				var reg = /^1[3-9]\d{9}$/;
				if (!this.form.mobile) {
					this.$message.warning("手机号不能为空");
				} else if (!reg.test(this.form.mobile)) {
					this.$message.error("手机号格式不正确");
				} else {
					this.getCodes(this.form.mobile);
				}
			},
			handleNameChange() {
				this.loginForm.username = this.loginForm.username.replace(/[^\d]/g, "");
			},
			handleMobileChange() {
				this.form.mobile = this.form.mobile.replace(/[^\d]/g, "");
			},
			onSuccess() {
				this.hasVerify = true;
			},
			showPwd() {
				if (this.passwordType === 'password') {
					this.passwordType = ''
				} else {
					this.passwordType = 'password'
				}
				this.$nextTick(() => {
					this.$refs.password.focus()
				})
			},
			handleLogin() {
				this.$refs.loginForm.validate(valid => {
					if (valid) {
						this.loading = true;
						this.$store.dispatch('user/login', {
							username: this.loginForm.username,
							password: this.$md5(this.loginForm.password)
						}).then(() => {
							this.$router.push({
								path: this.redirect || '/'
							})
							this.loading = false
						}).catch((error) => {
							this.loading = false;
						})
					} else {
						this.$message({
							message: "请检查表单错误",
							type: "error"
						});
						return false
					}
				})
			}
		}
	}
</script>

<style lang="scss">
	@supports (-webkit-mask: none) and (not (cater-color: #ffffff)) {
		.login-container .el-input input {
			color: #ffffff;
		}
	}

	.login-container {
		min-height: 100%;
		width: 100%;
		background-color: #ffffff;
		overflow: hidden;

		.el-input {
			display: inline-block;
			height: 47px;
			width: 85%;

			input {
				background: transparent;
				border: 0px;
				-webkit-appearance: none;
				border-radius: 0px;
				padding: 12px 5px 12px 15px;
				color: #000;
				height: 47px;
				// caret-color: #ffffff;
			}
		}

		.el-form-item {
			border: 1px solid #dddddd;
			background: #ffffff;
			border-radius: 5px;
			color: #454545;
		}
	}
</style>
<style lang="scss" scoped>
	.border_box {
		padding: 0 12px;
		border: 1px solid #eeeeee;
		border-radius: 3px;

		/deep/.el-input__inner {
			border: none;
		}

		svg {
			vertical-align: middle;
		}
	}

	.code_btn {
		width: 130px;
		margin-left: 20px;
		line-height: 45px;
		color: $mainBlue;
		border: 1px solid $mainBlue;
		border-radius: 3px;
	}

	.login-container {
		/deep/.el-form-item__content {
			line-height: 30px;
		}

		.login_top {
			height: 75px;
			width: 1200px;
			margin: 0 auto;
			position: relative;

			.left_logo {
				height: 50px;
				width: 180px;
				margin-top: 15px;
				background: url(../../assets/images/login_logo.png) no-repeat;
				background-size: 100%;
			}

			.right_link1 {
				cursor: pointer;
				font-size: 16px;
				line-height: 75px;
				float: right;
				margin-left: 45px;

				:hover {
					color: #53a6ff;
				}
			}

			.right_link2 {
				cursor: pointer;
				font-size: 16px;
				float: right;
				line-height: 75px;

				:hover {
					color: #53a6ff;
				}
			}
		}

		.login_middle {
			display: flex;
			justify-content: flex-end;
			align-items: center;
			width: 100%;
			min-height: 700px;
			margin: 0 auto;
			background: url(../../assets/images/login_bg.png) no-repeat center;
			background-size: cover;
			text-align: center;
			// height: 800px;
		}

		.login_footer {
			// width: 1200px;
			// margin: 0 auto;
			text-align: center;
			// margin-top: 620px;
			font-size: 14px;
			font-family: Microsoft YaHei;
			font-weight: 400;
			color: #999999;
			text-decoration: none;
			margin: 20px 0;

			a:hover {
				color: #53a6ff;
			}
		}

		.formClass {
			// margin: -600px auto;
			// width: 1200px;
			// position: relative;
		}

		.login_form,
		.forget_form {
			width: 350px;
			margin-right: 150px;
			max-width: 100%;
			padding: 30px 30px 15px;
			// margin: 0 auto;
			overflow: hidden;
			background-color: #ffffff;
			-webkit-border-radius: 5px;
			-moz-border-radius: 5px;
			background-clip: padding-box;
			// position: absolute;
			// top: 100px;
			// right: 0px;
		}

		.tips {
			font-size: 14px;
			color: #fff;
			margin-bottom: 10px;

			span {
				&:first-of-type {
					margin-right: 16px;
				}
			}
		}

		.svg-container {
			padding: 6px 5px 6px 15px;
			color: #889aa4;
			vertical-align: middle;
			width: 30px;
			display: inline-block;
		}

		.title-container {
			position: relative;

			.title {
				font-size: 26px;
				color: #333;
				margin: 0px auto 40px auto;
				text-align: center;
				font-weight: bold;
			}
		}

		.show-pwd {
			position: absolute;
			right: 10px;
			top: 7px;
			font-size: 16px;
			color: #889aa4;
			cursor: pointer;
			user-select: none;
		}
	}
</style>
